//
//  ProgressViewController.m
//  Project two framework
//
//  Created by 张峥 on 16/7/21.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ProgressViewController.h"
#import "ZZConfig.h"
#import "ProgressView.h"
#import "ProgressDetailView.h"

@interface ProgressViewController ()<UITextFieldDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *backgroundImage;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *progressImage;
@property (nonatomic, strong) UIImageView *searchTextField;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *keyword;
@property (nonatomic, strong) UIButton *searchImage;
@property (nonatomic, strong) ProgressView *progressView;
@property (nonatomic, strong) ProgressDetailView *detailView;
@end

@implementation ProgressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.backgroundImage];
    [self.backgroundImage addSubview:self.imageView];
    [self.imageView addSubview:self.progressImage];
    [self.imageView addSubview:self.keyword];
    [self.imageView addSubview:self.searchTextField];
    [self.searchTextField addSubview:self.textField];
    [self.searchTextField addSubview:self.searchImage];
    
    [self.scrollView addSubview:self.progressView];
    [self.scrollView addSubview:self.detailView];
    
    //注册键盘出现的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    //注册键盘消失的通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpAction:) name:@"notification" object:nil];
}
/** 键盘出现 */
- (void)keyboardWasShown:(NSNotification *)aNotification

{
    _imageView.frame = CGRectMake(20, -20, SCREEN_WIDTH - 120, SCREEN_HEIGHT - 120);
}
/** 键盘消失 */
- (void)keyboardWasHidden:(NSNotification *)aNotification
{
    _imageView.frame = CGRectMake(20, 20, SCREEN_WIDTH - 120, SCREEN_HEIGHT - 120);
    [self searchAction];
}
/** 点击tableViewCell的时候进行跳转 */
- (void)jumpAction:(NSNotification *)info
{
    NSArray *array = @[@"1_19",@"2_19",@"3_19"];
    _detailView.progress = info.userInfo[@"status"];
    NSInteger index = _detailView.progress.integerValue - 1;
    _detailView.statusImage.image = [UIImage imageNamed:array[index]];
    [UIView animateWithDuration:0.8 animations:^{
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * 2, 0)];
    }];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - 按钮的实现
- (void)backAction:(UIButton *)sender
{
    [UIView animateWithDuration:0.8 animations:^{
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * sender.tag, 0)];
    }];
}


- (void)searchAction
{
    if (_textField.text.length != 0) {
        [_textField resignFirstResponder];
        _progressView.keyword = _textField.text;
        [self.progressView loadDataSource];
        [UIView animateWithDuration:0.8 animations:^{
            [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
        }];
        _textField.text = @"";
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    _imageView.frame = CGRectMake(20, 20, SCREEN_WIDTH - 120, SCREEN_HEIGHT - 120);
    [textField resignFirstResponder];
    _progressView.keyword = _textField.text;
    [self.progressView loadDataSource];
    [UIView animateWithDuration:0.8 animations:^{
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
    }];

    return YES;
}

#pragma mark - 懒加载

- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT - 80);
        _scrollView.scrollEnabled = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}


- (UIImageView *)backgroundImage
{
    if (_backgroundImage == nil) {
        _backgroundImage = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 3, SCREEN_HEIGHT - 80)];
            imageView.image = [UIImage imageNamed:@"背景.png"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });
    }
    return _backgroundImage;
}

- (UIImageView *)imageView
{
    if (_imageView == nil) {
        _imageView = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 120, SCREEN_HEIGHT - 120)];
            imageView.image = [UIImage imageNamed:@"di_05.png"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });
    }
    return _imageView;
}

- (UIImageView *)progressImage
{
    if (_progressImage == nil) {
        _progressImage =  ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(350 , 140, 200, 30)];
            imageView.image = [UIImage imageNamed:@"text_34.png"];
            imageView;
        });
    }
    return _progressImage;
}

- (UILabel *)keyword
{
    if (_keyword == nil) {
        _keyword = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(130, 260, 160, 40)];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:25];
            label.text = @"关键字搜索:";
            label;
        });
    }
    return _keyword;
}
- (UIImageView *)searchTextField
{
    if (_searchTextField == nil) {
        _searchTextField = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(300 , 260, 430, 40)];
            imageView.image = [UIImage imageNamed:@"search_48.png"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });
    }
    return _searchTextField;
}

- (UITextField *)textField
{
    if (_textField == nil) {
        _textField = ({
            UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, 380, 40)];
            textField.font = [UIFont systemFontOfSize:25];
            textField.placeholder = @"Search";
            textField.delegate  = self;
            textField;
        });
    }
    return _textField;
}
- (UIButton *)searchImage
{
    if (_searchImage == nil) {
        _searchImage = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(395, 5, 30, 30);
            [btn setImage:[UIImage imageNamed:@"search-1_76.png"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _searchImage;
}

- (ProgressView *)progressView
{
    if (_progressView == nil) {
        _progressView = ({
            ProgressView *view = [[ProgressView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH , 0, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 80)];
            [view.back addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
            view.back.tag = 0;
            view;
        });
    }
    return _progressView;
}

- (ProgressDetailView *)detailView
{
    if (_detailView == nil) {
        _detailView = ({
            ProgressDetailView *view = [[ProgressDetailView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 2 , 0, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 80)];
            [view.back addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
            view.back.tag = 1;
            view;
        });
    }
    return _detailView;
}
@end
